package com.ushio.gougouwangguo.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ushio.gougouwangguo.entity.mysql.Adventure;
import com.ushio.gougouwangguo.entity.mysql.AdventureLog;
import com.ushio.gougouwangguo.entity.qo.AdventureQo;
import com.ushio.gougouwangguo.entity.vo.AdventureTypeDispositionVo;
import com.ushio.gougouwangguo.entity.vo.AdventureTypeVo;
import com.ushio.gougouwangguo.mapper.AdventureMapper;
import com.ushio.gougouwangguo.service.AdventureService;
import com.ushio.gougouwangguo.service.AdventureTypeService;
import com.ushio.gougouwangguo.service.BattleLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Random;

import java.util.*;

@Service
public class AdventureServiceImpl extends ServiceImpl<AdventureMapper, Adventure> implements AdventureService {

    @Autowired
    AdventureMapper adventureMapper;

    @Autowired
    AdventureTypeService adventureTypeService;

    @Autowired
    AdventureLogServiceImpl adventureLogService;
    @Autowired
    BattleLogService battleLogService;
    @Override
    @Transactional
    public void saveAdventureLog(AdventureQo adventureQo) {
        //收集探险数据
        Adventure adventure = Adventure.builder().build();

        Integer adventureTypeId = adventureQo.getAdventureTypeId();

        adventure.setAdventureTypeId(adventureTypeId);

        adventure.setDogCharacterId(adventureQo.getDogCharacterId());
        //保存探险 探险开始
        adventureMapper.insert(adventure);
        //获取 探险类型信息 和 关于探险类型底下所有的配置
        AdventureTypeVo adventureType = adventureTypeService.getAdventureType(adventureTypeId);
        //取到探险配置
        List<AdventureTypeDispositionVo> adventureTypeDispositionVos = adventureType.getAdventureTypeDispositionVos();
        //取到这次探险的次数
        Integer triggerFrequency = adventureType.getTriggerFrequency();
        //获取到 这次探险的经历
        List<Integer> listId = determineTheSequenceOfAdventures(triggerFrequency,adventureTypeDispositionVos);
        //探险日志
        List<AdventureLog> adventureLogList = new ArrayList<>();

        for (int id : listId) {
            AdventureLog adventureLog = AdventureLog.builder().build();
            adventureLog.setAdventureId(adventure.getAdventureId());
            adventureLog.setAdventureTypeDispositionId(id);
            adventureLogList.add(adventureLog);
        }

        adventureLogService.saveBatch(adventureLogList);

        battleLogService.saveTheBattleLog(adventureLogList);


        System.out.println(adventureLogList);


    }

    public List<Integer> determineTheSequenceOfAdventures(int triggerFrequency,List<AdventureTypeDispositionVo> adventureTypeDispositionVos){

        Random random = new Random();

        List<Integer> result = new ArrayList<>();
        if (adventureTypeDispositionVos == null || adventureTypeDispositionVos.isEmpty()) {
            return result; // 空输入，返回空结果
        }

        // 构建累积分布 map
        NavigableMap<Double, Integer> map = new TreeMap<>();
        double totalProbability = 0.0;
        for (AdventureTypeDispositionVo vo : adventureTypeDispositionVos) {
            if (vo.getAdventureTypeDispositionProbability() != null && vo.getAdventureTypeDispositionProbability() > 0) {
                totalProbability += vo.getAdventureTypeDispositionProbability();
                map.put(totalProbability, vo.getAdventureTypeDispositionId());
                // key = 累积概率上界，value = id
            }
        }

        // 触发指定次数
        for (int i = 0; i < triggerFrequency; i++) {
            double r = random.nextDouble() * totalProbability; // 0 ~ totalProbability
            Map.Entry<Double, Integer> entry = map.higherEntry(r);
            if (entry != null) {
                result.add(entry.getValue());
            }
        }

        return result;
    }
}
